package com.achun.getcouponleague.ui.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.achun.getcouponleague.R;
import com.achun.getcouponleague.base.BaseFragment;
import com.achun.getcouponleague.base.ISwitchfragment;
import com.achun.getcouponleague.ui.fragment.Home_Fragment;
import com.achun.getcouponleague.ui.fragment.Red_Packet_Fragment;
import com.achun.getcouponleague.ui.fragment.Search_Fragment;
import com.achun.getcouponleague.ui.fragment.Select_Fragment;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.PresenterManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements ISwitchfragment {
    public static final String TAG="MainActivity";
    @BindView(R.id.main_navigation_bar)
    public BottomNavigationView main_navigation_bar;
    private Home_Fragment home_fragment;
    private Select_Fragment select_fragment;
    private Red_Packet_Fragment red_packet_fragment;
    private Search_Fragment search_fragment;
    private FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        initEven();
    }

    private void initEven() {
        main_navigation_bar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.home:
                        Logutils.e(MainActivity.class,"onNavigationItemSelected: --》主页");
                        switchFragment(home_fragment);
                        break;
                    case R.id.search:
                        Logutils.e(MainActivity.class,"onNavigationItemSelected: --》搜索");
                        switchFragment(search_fragment);
                        break;
                    case R.id.red_packet:
                        Logutils.e(MainActivity.class,"onNavigationItemSelected: --》特惠");
                        switchFragment(red_packet_fragment);
                        break;
                    case R.id.selected:
                        Logutils.e(MainActivity.class,"onNavigationItemSelected: --》精选");
                        switchFragment(select_fragment);
                        break;
                }
//设置是否哦切换选中
                return true;
            }
        });
    }
    private BaseFragment lastPageFragment=null;
    private void switchFragment(BaseFragment targetfragment) {
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        if (!targetfragment.isAdded()) {
            fragmentTransaction.add(R.id.change_fragment, targetfragment);
        }
        if (lastPageFragment!=null){
            fragmentTransaction.hide(lastPageFragment);
        }
        fragmentTransaction.show(targetfragment);
        lastPageFragment=targetfragment;


        fragmentTransaction.commit();
    }

    private void initView() {
        home_fragment = new Home_Fragment();
        select_fragment = new Select_Fragment();
        red_packet_fragment = new Red_Packet_Fragment();
        search_fragment = new Search_Fragment();
        fm = getSupportFragmentManager();
        switchFragment(home_fragment);

    }

    @Override
    public void switchFragment() {
        main_navigation_bar.setSelectedItemId(R.id.search);
    }
}